package com.hd.pzhwlxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.pzhwlxt.mapper.RepairWokerMapper;
import com.hd.pzhwlxt.pojo.Materialprice;
import com.hd.pzhwlxt.pojo.RepairWoker;
import com.hd.pzhwlxt.pojo.VehicleRepairInfo;
import com.hd.pzhwlxt.pojo.VehicleRepairMaterialInfo;
import com.hd.pzhwlxt.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @create: 2022-09-21 9:01
 * @author: 怀念天空
 * @Description:
 */

@Service
public class RepairWokerServiceImpl extends ServiceImpl<RepairWokerMapper, RepairWoker> implements RepairWokerService {

    @Autowired
    private RepairWokerMapper repairWokerMapper;

    @Autowired
    private VehicleRepairInfoService vehicleRepairInfoService;

    @Autowired
    private VehicleInfoService vehicleInfoService;

    @Autowired
    private MaterialpriceService materialpriceService;
    @Autowired
    private VehicleRepairMaterialInfoService vehicleRepairMaterialInfoService;


    /**
     * 添加维修记录，同时添加车辆维修记录。
     */
    @Override

    public boolean addRepairInfo(Object[] requestBody) {
        // 添加维修信息,系统生成订单编号
        String uuid = UUID.randomUUID().toString().replace("-","");
        RepairWoker repairWoker = new RepairWoker();
        // 获取维修信息
        Map<String, Object> map = (Map<String, Object>) requestBody[1];
        Integer id = (Integer) requestBody[2];//车辆id
        String address = (String) requestBody[3];//地址
        String vehicleId = vehicleInfoService.getById(id).getVehicleId();


        Date inTime = null;
        Date outTime = null;
        try {
            inTime = new SimpleDateFormat("yyyy-MM-dd").parse((String) map.get("inTime"));
            outTime = new SimpleDateFormat("yyyy-MM-dd").parse((String) map.get("outTime"));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        String reason = (String) map.get("reason");
        Integer repairman =  (Integer) map.get("repairman");
        String handlePeople = (String) map.get("handlePeople");
        String remark = (String) map.get("remark");
        Double worktimeBills = Double.parseDouble((String) map.get("worktimeBills")) ;
        Double materialBills = 0.0;

        repairWoker.setVehicleId(vehicleId);// 车牌
        repairWoker.setRepairDate(inTime);
        repairWoker.setAddress(address);// 地点
        repairWoker.setReason(reason);// 原因
        repairWoker.setWoker(repairman);// 维修工
        repairWoker.setRepairId(uuid);// 订单
        repairWoker.setAcceptor(handlePeople);// 受理人
        repairWoker.setFinishDate(outTime);
        repairWoker.setRemark(remark);

        // 封装消耗物资列表
        List<VehicleRepairMaterialInfo> list = new ArrayList<>();
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) requestBody[0];
        for (int i = 0; i < mapList.size(); i++) {
            VehicleRepairMaterialInfo repairMaterialInfo = new VehicleRepairMaterialInfo();
            Integer materialId = (Integer) mapList.get(i).get("value");
            Integer num = (Integer) mapList.get(i).get("num");
            repairMaterialInfo.setMaterialId(materialId);
            repairMaterialInfo.setRepairVehicleRecordId(uuid);
            repairMaterialInfo.setUseTime(inTime);
            repairMaterialInfo.setUseCount(num);
            repairMaterialInfo.setHandlePeople(handlePeople);
            list.add(repairMaterialInfo);

            QueryWrapper<Materialprice> wrapper = new QueryWrapper<>();
            wrapper.eq("material_id",materialId);
            Materialprice materialprice = materialpriceService.getOne(wrapper);
            materialBills += materialprice.getInPrice().doubleValue() * num;
        }
        //添加耗材记录
        vehicleRepairMaterialInfoService.saveBatch(list);
        VehicleRepairInfo vehicleRepairInfo = new VehicleRepairInfo();
        vehicleRepairInfo.setRepairId(uuid);
        //材料费
        vehicleRepairInfo.setMaterialBills(materialBills);
        // 获取维修车牌号
        vehicleRepairInfo.setVehicleId(vehicleId);
        vehicleRepairInfo.setWorktimeBills(worktimeBills);
        vehicleRepairInfo.setInTime(inTime);
        vehicleRepairInfo.setOutTime(outTime);
        vehicleRepairInfo.setOutCheckPeople(handlePeople);
        vehicleRepairInfo.setRepairman(repairman);
        vehicleRepairInfo.setReason(reason);
        vehicleRepairInfo.setRemark(remark);
        vehicleRepairInfo.setHandlePeople(handlePeople);
        vehicleRepairInfoService.save(vehicleRepairInfo);
        return repairWokerMapper.insert(repairWoker) > 0;
    }

    @Override
    public List<RepairWoker> getAllWorkerRepairInfo() {
        return repairWokerMapper.getAllWorkerAndVehicleInfo();
    }

    @Override
    public Page<RepairWoker> getAllWorkerRepairInfoPage(Integer pageNum, Integer pageSize) {
        return repairWokerMapper.getAllWorkerAndVehicleInfoPage(new Page<RepairWoker>(pageNum, pageSize));
    }
}
